package com.changgou.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.Result;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.page.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.thymeleaf.ITemplateEngine;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {


    @Autowired
    private SpuFeign spuFeign;

    @Autowired
    private CategoryFeign categoryFeign;

    @Autowired
    private SkuFeign skuFeign;

    @Value("${pagepath}")
    private String pagepath;

    @Autowired
    private TemplateEngine templateEngine;

    @Override
    public void generateHtml(String id) {
        // 1.1创建context对象，用于存储商品的相关数据
        Context context = new Context();
        Map<String, Object> itemDate = getItemDate(id);
        context.setVariables(itemDate);

        // 1.2获取商品详情页面的存储位置
        File dir = new File(pagepath);
        // 1.3判断当前存储位置的文件夹是否存在。如果不存在则创建
        if (!dir.exists()){
            dir.mkdirs();
        }
        // 1.4定义输出流，完成文件的生成
        File file = new File(dir+"/"+id+".html");
        Writer out = null;

        try{
            out = new PrintWriter(file);

            templateEngine.process("item",context,out);
        }catch (Exception e){
            e.printStackTrace();

        }finally {
            // 1.5关闭流
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }




    }

    private Map<String,Object> getItemDate(String id){

        Map<String,Object> map = new HashMap<>();

        Spu spu = spuFeign.findSpuById(id).getData();
        map.put("spu",spu);

        if (spu != null){
            if (!StringUtils.isEmpty(spu.getImages())){
                String[] split = spu.getImages().split(",");
                map.put("imageList",split);
            }
        }

        // 获取商品分类信息
        Category category1 = categoryFeign.findById(spu.getCategory1Id()).getData();
        Category category2 = categoryFeign.findById(spu.getCategory2Id()).getData();
        Category category3 = categoryFeign.findById(spu.getCategory3Id()).getData();

        map.put("category1",category1);
        map.put("category2",category2);
        map.put("category3",category3);

        // 获取sku相关信息
        List<Sku> list = skuFeign.findSkuListBySpuId(id);
        map.put("skuList",list);

        String specItems = spu.getSpecItems();
        Map specificationList = JSON.parseObject(specItems, Map.class);
        map.put("specificationList",specificationList);

        return map;

    }
}
